package ru.smc.hogwarts.servlets.expulsion;

import java.io.IOException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ru.smc.hogwarts.classes.EMF;
import ru.smc.hogwarts.model.Student;

public class ExpulsionList extends HttpServlet implements Servlet {
	
	private static final long serialVersionUID = 1L;	
	private HttpSession session;
       
    public ExpulsionList() {
        super();
    }
    
	@SuppressWarnings("unchecked")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		session = request.getSession(true);
    	
		EntityManagerFactory emf = EMF.getInstance().getEMF();
    	EntityManager em = emf.createEntityManager();
    	String sid = request.getParameter("id");
		
    	if (sid!= null){
			em.getTransaction().begin();
			Student student = em.find(Student.class, sid);
			student.setStatus("отчислен");
			em.persist(student);
			em.getTransaction().commit();
		}
		
    	String action = request.getParameter("search");
    					
		if (action != null && action.equals("clear") || (session.getAttribute("servletName") != null && !session.getAttribute("servletName").equals(getServletName()))) {
			session.setAttribute("searchFN", "");
			session.setAttribute("searchLN", "");
	    	session.setAttribute("searchY", "");
	    	session.setAttribute("searchH", "");
		}
		
		session.setAttribute("servletName", getServletName());
    			
    	String select = "select distinct s from Student s, Assessment a " +
        "where s.status = 'обучается' " +
        "and s.id = a.student.id " +
        "and (a.grade = 'С' " +
        "or a.grade = 'О' " +
        "or a.grade = 'Т')";
    	
    	if ((session.getAttribute("searchFN") != null) && (session.getAttribute("searchFN") != "")) 
    		select += " and s.person.firstName like '%" + session.getAttribute("searchFN") + "%'";
    	if ((session.getAttribute("searchLN") != null) && (session.getAttribute("searchLN") != "")) 
    		select += "and s.person.lastName like '%" + session.getAttribute("searchLN") + "%'";
    	if ((session.getAttribute("searchY") != null) && (session.getAttribute("searchY") != "")) 
    		select += "and s.year = " + session.getAttribute("searchY");
    	if ((session.getAttribute("searchH") != null) && (session.getAttribute("searchH") != "")) 
    		select += "and s.person.house.name = '" + session.getAttribute("searchH") + "'";    	
    	
    	Query query = em.createQuery(select);
    	int size = query.getResultList().size();
    	
    	request.setAttribute("size", size);
    	boolean expltime = (Boolean) session.getAttribute("expulsionTime");
		
    	if (size != 0 && expltime) { 
	    	action = request.getParameter("page");
	    	
			if (action == null) {
				session.setAttribute("pageBegin", 0);
	    		session.setAttribute("pageEnd", 19);
			} else {
	    		int begin = 0;
	    		int end = 19;
	    		
	    		try {
	    			begin = (Integer)session.getAttribute("pageBegin");
	    			end = (Integer)session.getAttribute("pageEnd");
	    		} catch (NullPointerException e) {}
	    		
	    		if (action.equals("next") && end+1 < size) {
	    			begin += 20;
	    			end += 20;
	    		} else if (action.equals("previous") && (begin > 0)) {
	    			begin -= 20;
	    			end -= 20;
	    		}
	    		
	    		session.setAttribute("pageBegin", begin);
	    		session.setAttribute("pageEnd", end);
	    	}
			
			
			query.setFirstResult((Integer)session.getAttribute("pageBegin"));
	    	query.setMaxResults(20);
	    	List<Student> students = query.getResultList();
	    	
	    	request.setAttribute("students", students);
    	}
	
	    request.getRequestDispatcher("/pages/expulsion/exStudentsList.jsp").forward(request, response);
    	
    	em.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		session.setAttribute("searchFN", request.getParameter("searchFN"));
		session.setAttribute("searchLN", request.getParameter("searchLN"));
		session.setAttribute("searchY", request.getParameter("searchY"));
		session.setAttribute("searchH", request.getParameter("searchH"));
		
		this.doGet(request, response);
	}

}
